<?php

declare (strict_types = 1);

namespace app\matter\controller;

use app\matter\BaseController;
use app\matter\model\MatterDirectoryModel;
use think\facade\Db;
use think\facade\View;

//目录管理
class MatterDirectory extends BaseController
{
    public function index()
    {
        if (request()->isAjax()) {
            $param = get_params();
            $where = array();
            if (!empty($param['keywords'])) {
                $where[] = ['id|name', 'like', '%' . $param['keywords'] . '%'];
            }
            if (!empty($param['area_id'])) {
                $where[] = ['area_id', '=', $param['area_id']];
            }else{
                $where[] = ['area_id', '=', $this->area_id];
            }
            if (!empty($param['unit_id'])) {
                $where[] = ['unit_id', '=', $param['unit_id']];
            }
            $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
            $list = MatterDirectoryModel::with(['unit'=>function($query){
                $query->field('id,name');
            }])->where($where)->paginate($rows, false, ['query' => $param])->toArray();

            return table_assign(0, '', $list);
        } else {

            return view();
        }
    }

    //添加部门
    public function add()
    {

        $param = get_params();
        if (request()->isAjax()) {
            if ($param['id'] > 0) {

                $department_array = get_area_son($param['id']);
                if (in_array($param['pid'], $department_array)) {
                    return to_assign(1, '上级目录不能是该目录本身或其下属目录');
                } else {
                    MatterDirectoryModel::update($param);
                    add_log('edit', $param['id'], $param);
                    return to_assign();
                }
            } else {
                $model = new MatterDirectoryModel();
                $param['uid'] = $this->uid;
                $model->save($param);
                add_log('add', $model->id, $param);
                return to_assign();
            }
        } else {
            $id = isset($param['id']) ? $param['id'] : 0;
            $pid = isset($param['pid']) ? $param['pid'] : 0;
            $area_list = set_recursion2(get_area($this->area_id));
            if ($id > 0) {
                $detail = MatterDirectoryModel::find($id);
                if(!empty($detail['unit_id'])){
                    $area_id_unit = Db::table('m_unit')->where('id',$detail['unit_id'])->value('area_id');
                    $unit_list = get_unit_list($area_id_unit);
                    View::assign('unit_list', $unit_list);
                }
                View::assign('detail', $detail);
            }else{
                $unit_list = get_unit_list($this->area_id);
                View::assign('unit_list', $unit_list);
            }
            View::assign('area_list', $area_list);
            View::assign('pid', $pid);
            View::assign('id', $id);
            return view();
        }
    }

    //删除
    public function delete()
    {
        $id = get_params("id");
        $count = MatterDirectoryModel::where(['pid', '=', $id])->count();
        if ($count > 0) {
            return to_assign(1, "该区域下还有子区域，无法删除");
        }
        $users = Db::name('Admin')->where([['area_id', '=', $id], ['status', '>=', 0]])->count();
        if ($users > 0) {
            return to_assign(1, "该区域下还有员工，无法删除");
        }
        if (MatterDirectoryModel::delete($id) !== false) {
            add_log('delete', $id);
            return to_assign(0, "删除成功");
        } else {
            return to_assign(1, "删除失败");
        }
    }
}
